<?php
 header('content-type:text/html;charset=utf-8'); 
	$lot=$_GET["lot"];
	$dbName = "sqlsrv:Server=172.31.3.9,1433;Database=UFDATA_001_2017";   //emc3030
	$dbUser = "sa";    //
	$dbPassword = "TDled2017";    //
	$db = new PDO($dbName, $dbUser, $dbPassword);   
	//$sql = "select MoId from  mom_order where MoCode ='".$lot."' ";
	//print_r($sql);
	//$res = $db->query($sql);
	//print_r($res);
	// while ($row = $res->fetch()){
	//	print_r($MoDId=$row['MoId']);
	//}	
	$sql = "select MoDId,InvCode from mom_orderdetail where moid=(select MoId from  mom_order where MoCode ='".$lot."') ";  //
	//print_r($sql);
	$res = $db->query($sql);
	//print_r($res);
	$MoDId="";
	$InvCode="";
	//$row = null;
	 while ($row = $res->fetch()){
		$MoDId=$row['MoDId'];
		$InvCode=$row['InvCode'];
	}
	
	$sql = "select invcode,Qty from mom_moallocate where MoDId = '".$MoDId."' and invcode like 'A%' ";//17620T4003-7250613
	$res2 = $db->query($sql);
	//
	$sub_invcode="";
	$Qty="";
    $i=0;
	
	while ($row = $res2->fetch()){
	  $i+=1;
	  if ($i==1){//取芯片1料號和數量
		$sub_invcode1= $row['invcode'];
		$Qty=$row['Qty'];
	  };
	  if ($i==2){//取芯片2料號和數量
		$sub_invcode2= $row['invcode'];
		$Qty+=$row['Qty'];
	  };
	}
	//取芯片1料號明細
	
	$sql = "select cInvName,cInvDefine10,cInvDefine3,cInvDefine2 from Inventory where cinvcode='".$sub_invcode1."' ";
	$res = $db->query($sql);
	$cInvName="";
	$ele="";
	$light="";
	$wave="";
	while ($row = $res->fetch()){
	  $cInvName= $row['cInvName'];//芯片名
	  $ele=$row['cInvDefine10'];//电压
	  $light= $row['cInvDefine3'];//亮度
	  $wave=$row['cInvDefine2'];//波长
	}
	
	if ($i>=2){//取芯片2料號明細
		$sql = "select cInvName,cInvDefine10,cInvDefine3,cInvDefine2 from Inventory where cinvcode='".$sub_invcode2."' ";
		$res = $db->query($sql);
		$cInvName2="";
		$ele2="";
		$light2="";
		$wave2="";
		while ($row = $res->fetch()){
		  $cInvName2= $row['cInvName'];//芯片名
		  $ele2=$row['cInvDefine10'];//电压
		  $light2= $row['cInvDefine3'];//亮度
		  $wave2=$row['cInvDefine2'];//波长
		}
	};
	
	$res = null;
	$res2 = null;
	$db = null;
	if ($i>=2){//芯片料號個數大於1時，就湊下面這句
		$wo= $InvCode.",".$sub_invcode1.";".$sub_invcode2.",".$Qty.",".$cInvName2.",".$ele.";".$ele2.",".$light.";".$light2.",".$wave.";".$wave2;
	}else{//否則就原來湊的那樣
		$wo= $InvCode.",".$sub_invcode1.",".$Qty.",".$cInvName.",".$ele.",".$light.",".$wave;
	};
	//T2027911A-M6701A,A233000;A2330A0,760.512,晶片/S-30MBMUD;晶片/S-30MBMUD,2.8-3.1;3.1-3.2,155-162;155-162,452.5-455
	print_r($wo);
?>